package com.heba.popupwindow;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.heba.adapter.QuickAdapter;
import com.heba.bean.user.UserAddressObject;
import com.heba.bean.user.UserObject;
import com.heba.config.CommonConfig;
import com.heba.event.MessageEvent;
import com.heba.fragment.business.BusinessMapDetailActivity;
import com.heba.fragment.product.ShopListFragment;
import com.heba.greenmulu.R;
import com.heba.popupwindow.callback.EditAddressClickListener;

import net.sourceforge.zbar.Image;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class ShopAddressPopupWindow extends  BasePopupWindow{
    private RecyclerView recyclerView;
    public QuickAdapter recycleAdapter;;
    public List<UserAddressObject> list ;
    public UserObject userObject;
    private View dismiss_view;
    private Context context;
    private TextView addtextview;
    public ConstraintLayout constrain_layout;
    public ImageView cycle_imageview;

    public ShopListFragment shopListFragment;
    private EditAddressClickListener clickListener;
    public ShopAddressPopupWindow(Context context, int width, int height) {
        super(context, LayoutInflater.from(context).inflate(
                R.layout.shop_popup_edit_address_view, null), width, height);
        setAnimationStyle(R.style.Popup_Animation_Translate);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        list = new ArrayList<>();
        initViews();
        this.context = context;
    }
    public ShopAddressPopupWindow(Context context) {
        super(LayoutInflater.from(context).inflate(
                R.layout.shop_popup_edit_address_view, null));
        setAnimationStyle(R.style.Popup_Animation_Translate);
    }

    public List<UserAddressObject> getList() {
        return list;
    }

    public void setList(List list1){
        list = list1;
        recycleAdapter = new QuickAdapter<UserAddressObject>(list){

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.recycle_address_row;
            }

            @Override
            public void convert(VH holder, UserAddressObject data, int position) {
                View layout = holder.getView(R.id.constrain_layout);
                View linearLayout =  layout.findViewById(R.id.linear_layout);
                ImageView selectImage = (ImageView) linearLayout.findViewById(R.id.cycle_imageview);

                    UserAddressObject dataObj = list.get(position);
                    LinearLayout linearLayout1 = layout.findViewById(R.id.content_txt);
                    linearLayout1.setVisibility(View.VISIBLE);
                    TextView textView = layout.findViewById(R.id.now_location);
                    textView.setVisibility(View.INVISIBLE);
                    if (dataObj.getIsSelected().equals("1")) {
                        selectImage.setImageResource(R.drawable.guide_point_green);
                    } else {
                        selectImage.setImageResource(R.drawable.icon_yuanquan_fill);
                    }
                    TextView addressTV = (TextView) linearLayout.findViewById(R.id.adress_textview);
                    TextView phoneTV = (TextView) linearLayout.findViewById(R.id.phone_textview);
                    ImageView editTV = (ImageView) linearLayout.findViewById(R.id.edit_textview);
//                    editTV.setOnClickListener(v -> {
//                        clickListener.clickEditAddress(dataObj);
//                    });

                    addressTV.setText(dataObj.getDistrict()+dataObj.getStreet()+dataObj.getEstate()+dataObj.getBuilding()+","+dataObj.getFloor()+context.getResources().getString(R.string.Floor)+","+dataObj.getUnit()+context.getResources().getString(R.string.Unit));
                    phoneTV.setText("+852 " + dataObj.getPhone());
                    layout.setOnClickListener(v -> {
                        clickListener.clickRowToSelect(dataObj);
                        recycleAdapter.notifyDataSetChanged();
                    });
                

            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        };

        recyclerView.setAdapter(recycleAdapter);
        recycleAdapter.notifyDataSetChanged();
    }
    @Override
    public void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        dismiss_view = findViewById(R.id.dismiss_view);
        addtextview = (TextView) findViewById(R.id.add_text_view);
        addtextview.setOnClickListener(v->{
            clickListener.clickAddNewAddress();
        });
        cycle_imageview = (ImageView)findViewById(R.id.cycle_imageview);
        constrain_layout = (ConstraintLayout) findViewById(R.id.constrain_layout);

        dismiss_view.setOnClickListener(v -> {
            super.dismiss();
        });
//        setList();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        /**
         * 接收事件
         */

        if (event.getEvent().equals(CommonConfig.UPDATE_LOCATION)){

        }
    }
    @Override
    public void initEvents() {

    }

    @Override
    public void init() {

    }
    public void setClickListener(EditAddressClickListener clickListener) {
        this.clickListener = clickListener;
    }


}
